<template>
	<view class="content padding">
		<view class="head bg-img">
			<text>{{ smmUser.score != null ? smmUser.score : '重进以刷新' }}</text>
		</view>
		<view class="inquire flex justify-between">
			<view class="flex align-center padding-lr"><input type="text" v-model="queryId" class="response" placeholder="输入用户ID" /></view>
			<view class="flex align-center justify-center" @click="changed()">查询</view>
		</view>
		<view class="flex flex-direction align-center justify-center info" >
			<image v-if="obj.avatarUrl == null" src="" mode="" class="round solid bg-yellow" style="width: 120rpx; height: 120rpx;"></image>
			<image
				v-if="obj.avatarUrl != null"
				:src="obj.avatarUrl"
				mode=""
				class="round bg-gradual-green"
				style="width: 120rpx; height: 120rpx;"
			></image>
			<text class="name ">{{ obj.nickName || '' }}</text>
			<text class="idCard">ID: {{ obj.id || '请先查询' }}</text>
			<view class="inp flex align-center justify-center padding-lr">
				<input type="text" v-model="giveScore" class="response flex justify-center" placeholder="输入赠送积分数量" />
			</view>
			<u-toast ref="uToast" />
			<button class="cu-btn buy " @click="give()">立即赠送</button>
		</view>
	</view>
</template>

<script>
const app = getApp();
export default {
	data() {
		return {
			queryId: null,
			giveScore: null,
			obj: {},
			smmUser: {},
			show: 0
		};
	},
	onLoad() {
		var that = this;
		app.globalData.api.getUser().then(res => {
			that.smmUser = res.resultObj.user;
		});
	},
	methods: {
		changed() {
			if (!this.queryId) return this.$refs.uToast.show({ title: '请输入用户的ID~~', type: 'error' });

			var that = this;
			app.globalData.api.getUserById(this.queryId).then(res => {
				if (res.resultObj != null) {
					console.log("1111: ",1111);
					this.$refs.uToast.show({ title: '查询成功~~', type: 'success' });
					that.show = 1;
					that.obj = res.resultObj;
				} else {
					console.log("22222: ",22222);
					this.queryId = '';
					this.$refs.uToast.show({ title: '请重新填写ID~~', type: 'error' });
				}
			});
		},
		give() {
			if (this.giveScore <= 0) {
				this.$refs.uToast.show({ title: '请输入正确的积分~~', type: 'error' });
			} else if (this.obj.id == null) {
				this.$refs.uToast.show({ title: '请选择赠送的对象~~', type: 'error' });
			} else if (this.smmUser.score < this.giveScore || this.smmUser.score <= 0) {
				this.$refs.uToast.show({ title: '您的积分不足~~', type: 'error' });
			} else {
				var that = this;
				var userScore = {
					userId: app.globalData.smmUser.id,
					score: that.giveScore,
					comment: '赠送用户ID:' + that.obj.id + '的用户' + that.giveScore + '积分',
					objId: that.obj.id,
					isGet: 0
				};
				app.globalData.api.giveUserScore(userScore).then(res => {
					if (res.resultObj > 0) {
						this.giveScore = '';
						this.$refs.uToast.show({ title: '赠送成功~~', type: 'success' });
						// app.globalData.api.getUser().then(res2 => {
						// 	app.globalData.smmUser = res2.resultObj;
						// 	that.smmUser = res2.resultObj;
						// });

						app.globalData.api.getUser().then(res => {
							that.smmUser = res.resultObj.user;
						});
					}
				});
			}
		}
	}
};
</script>

<style lang="scss" scoped>
.content {
	.head {
		width: 690rpx;
		height: 330rpx;

		border-radius: 20rpx;
		margin: 0 auto;
		background-image: url('https://z3.ax1x.com/2021/04/02/ceM7n0.png');
		padding: 120rpx 0 0 60rpx;
		text {
			font-size: 80rpx;
			font-family: PingFang SC;
			font-weight: 800;
			color: #ffffff;
		}
	}
	.inquire {
		margin: 70rpx 0;
		view:nth-child(1) {
			width: 450rpx;
			height: 80rpx;
			border: 1rpx solid #e6e6e6;
			border-radius: 10rpx;
		}
		view:nth-child(2) {
			width: 214rpx;
			height: 80rpx;
			background: #e6e6e6;
			border-radius: 10rpx;

			font-size: 32rpx;
			font-family: PingFang SC;
			font-weight: 500;
			color: #333333;
		}
	}
	.info {
		.name {
			font-size: 36rpx;
			font-family: PingFang SC;
			font-weight: bold;
			color: #333333;
			margin: 30rpx 0;
		}
		.idCard {
			font-size: 28rpx;
			font-family: PingFang SC;
			font-weight: 500;
			color: #333333;
		}
		.inp {
			width: 500rpx;
			height: 80rpx;
			border: 1rpx solid #e6e6e6;
			border-radius: 10rpx;
			margin: 60rpx 0;
		}
		.buy {
			width: 500rpx;
			height: 80rpx;
			background: #f8ce39;
			border-radius: 12rpx;

			font-size: 30rpx;
			font-family: PingFang SC;
			font-weight: 500;
			color: #333333;
		}
	}
}
</style>
